package com.jzo2o.market.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.model.dto.request.SeizeCouponReqDTO;
import com.jzo2o.market.model.dto.response.CouponInfoResDTO;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户端-优惠券活动相关接口
 *
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@RestController("marketConsumerController")
@RequestMapping("/consumer/coupon")
@Api(tags = "用户端-优惠券活动相关接口")
@Slf4j
public class MarketConsumerController {

    @Autowired
    private ICouponService couponService;

    /**
     * 我的优惠券列表接口
     * @param
     * @return
     */
    @GetMapping("/my")
    @ApiOperation("我的优惠券列表接口")
    public List<CouponInfoResDTO> selectMyActivity(Integer status, Long lastId) {
        List<Coupon> list = couponService.list(Wrappers.<Coupon>lambdaQuery()
                .eq(Coupon::getUserId, UserContext.currentUserId())
                .eq(Coupon::getStatus, status)
                .lt(ObjectUtils.isNotEmpty(lastId), Coupon::getId, lastId)
                .orderByDesc(Coupon::getId)// id倒序
                .last("limit 10"));

        // 使用Stream处理list集合
//        List<Coupon> filteredList = list.stream()
//                .filter(coupon -> ObjectUtil.isEmpty(lastId) || coupon.getId() > lastId) // 过滤掉lastId之前的数据
//                .limit(10) // 限制结果集最多10条记录
//                .collect(Collectors.toList());
        return BeanUtil.copyToList(list, CouponInfoResDTO.class);
    }

    /**
     * 抢券接口
     *
     * @param
     * @return
     */
    @PostMapping("/seize")
    @ApiOperation("抢券接口")
    public void snag(@RequestBody SeizeCouponReqDTO seizeCouponReqDTO) {
         couponService.snag(seizeCouponReqDTO);
    }
}
